package com.aispeech.detector;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.aispeech.detector.receiver.PowerStateReceiver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements OnItemClickListener {
    private final String TAG = "MainActivity";

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                onBackPressed();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setActionBar();

        ListView listView = findViewById(R.id.lv_items);
        ArrayList<HashMap<String, Object>> listItems = new ArrayList<>();

        HashMap<String, Object> item;

        item = new HashMap<>();
        item.put("activity_name", "系统信息");
        item.put("activity_class", SystemInfoActivity.class);
        listItems.add(item);

        item = new HashMap<>();
        item.put("activity_name", "AudioRecord录音测试");
        item.put("activity_class", AudioRecordActivity.class);
        listItems.add(item);

        item = new HashMap<>();
        item.put("activity_name", "遥控器录音测试");
        item.put("activity_class", RemoteRecActivity.class);
        listItems.add(item);

//        item = new HashMap<>();
//        item.put("activity_name", "Jni Record录音测试");
//        item.put("activity_class", AIJniRecordTestActivity.class);
//        listItems.add(item);

//        item = new HashMap<>();
//        item.put("activity_name", "串口测试");
//        item.put("activity_class", SerialActivity.class);
//        listItems.add(item);


        SimpleAdapter adapter = new SimpleAdapter(this, listItems, R.layout.act_list_item,
                new String[]{"activity_name"}, new int[]{R.id.tv_act_item});

        listView.setAdapter(adapter);
        listView.setDividerHeight(2);

        listView.setOnItemClickListener(this);
//        broadcastTest();
    }

//    private PowerStateReceiver mPowerStateReceiver;
//    private void broadcastTest() {
//        Log.d(TAG, "broadcastTest: ");
//        mPowerStateReceiver = new PowerStateReceiver();
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
//        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
//        registerReceiver(mPowerStateReceiver, intentFilter);
//    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        if (null != mPowerStateReceiver){
//            unregisterReceiver(mPowerStateReceiver);
//        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Map<?, ?> map = (HashMap<?, ?>) parent.getAdapter().getItem(position);
        Class<?> clazz = (Class<?>) map.get("activity_class");
        Intent it = new Intent(this, clazz);
        this.startActivity(it);
    }
}
